<?php
namespace app\common\model;

class Captcha extends Base
{
    public static function add($data)
    {
        $insert = [
            'type' => $data['type'],
            'code' => $data['code'],
            'send' => $data['send'],
            'time' => time(),
            'ip' => real_ip(),
            'status' => 0,
        ];
        $self = new static();
        return $self->insert($insert);
    }

    public static function send_captcha($number, $title, $content)
    {
        $self = new static();
        $send_code = rand(111111, 999999);
        if (check_mail($number)) {//邮箱
            $row = $self->
            where('send', '=', $number)->
            order('id', 'desc')->
            field('time')->find();
            if ($row['time'] > time() - 60) {
                $result['code'] = 1;
                $result['message'] = '发送邮件之间需要相隔60秒！';
                return $result;
            }
            $where['send'] = $number;
            $where['time'] = ['>', time() - 3600 * 24];
            $count = $self->
            where($where)->
            count();
            if ($count > 6) {
                $result['code'] = 1;
                $result['message'] = '该邮箱发送次数过多，请更换邮箱！';
                return $result;
            }
            $where['ip'] = real_ip();
            $where['time'] = ['>', time() - 3600 * 24];
            $count = $self->
            where($where)->
            count();
            if ($count > 10) {
                $result['code'] = 1;
                $result['message'] = '该邮箱今日发送次数过多，请更换邮箱！';
                return $result;
            }
            $sub = config('web_webname') . '-' . $title;
            if ($content) {
                $msg = $content;
            } else {
                $msg = get_mail_tempale(1, $title, $send_code);
            }
            $send_result = send_mail($number, $sub, $msg);
            if ($send_result) {
                $result = [
                    'code' => 0,
                    'message' => '验证码已成功发送至您的邮箱，请注意查收！'
                ];
                Captcha::add([
                        'type' => '1',
                        'code' => $send_code,
                        'send' => $number
                    ]
                );
            } else {
                $result['code'] = 1;
                $result['message'] = '发送邮件失败，请联系管理员！';
            }
            return $result;
        } else {
            $result['code'] = 1;
            $result['message'] = '邮箱格式不合法！';
        }
        return $result;
    }

    public static function check_captcha($number, $code)
    {
        $self = new static();
        $where['type'] = 1;
        $where['send'] = $number;
        $where['code'] = $code;
        $row = $self->where($where)->order('id', 'desc')->find();
        if (!$row) {
            $result['message'] = '验证码不正确！';
            return $result;
        } elseif ($row['time'] < time() - 300 || $row['status'] > 0) {
            $result['message'] = '验证码已失效，请重新获取！';
            return $result;
        }
    }
}